﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using CH.Reia.Entity;
using System.Configuration;

namespace CH.Reia.Mvc.Controllers
{
    [HandleError]
    public class MemberController : Controller
    {
        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(Member model)
        {
            ModelState.Clear();

            if (string.IsNullOrEmpty(model.UserName))
                ModelState.AddModelError("UserName", "Username é necessário!");

            if (string.IsNullOrEmpty(model.Password))
                ModelState.AddModelError("Password", "Password é necessário!");

            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.Authenticate(model.UserName, model.Password);
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    return RedirectToAction("Index", "Home");
                }
                else
                    ModelState.AddModelError("", "Username ou Password incorreto!");
            }
            return View(model);
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Member model)
        {
            ModelState.Clear();

            if (string.IsNullOrEmpty(model.UserName))
                ModelState.AddModelError("UserName", "Username é necessário!");

            if (string.IsNullOrEmpty(model.Email))
                ModelState.AddModelError("Email", "Email é necessário!");

            if (string.IsNullOrEmpty(model.Password))
                ModelState.AddModelError("Password", "Password é necessário!");

            if (ModelState.IsValid)
            {
                var reiaDomain = Reia.Domain.Domain.CreateDomain();
                var memberSave = new Member
                {
                    MemberId = Guid.NewGuid().ToString(),
                    UserName = model.UserName,
                    Password = model.Password,
                    CreateAt = DateTime.Now,
                    Domain = ConfigurationManager.AppSettings["UserDomain"]
                };

                if (reiaDomain.MemberSave(memberSave).Count > 0)
                {
                    FormsAuthentication.Authenticate(model.UserName, model.Password);
                    FormsAuthentication.SetAuthCookie(model.UserName, false);

                    return RedirectToAction("Index", "Home");
                }
            }
            return View(model);
        }
    }
}
